package com.tcl.bodycoach;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.VideoView;

import com.bytedance.labcv.effectsdk.SkeletonDetect;
import com.tcl.bodycoach.library.OrientationSensor;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    private SkeletonDetect mSkeletonDetector;
    private UnzipTask unzipTask;
    private static File sRootDir;
    private static final String SKELETON = "model/ttskeletonmodel/tt_skeleton_v6.0.model";
    public ImageView mImageView;
    private CameraSurfaceView mSurfaceView;
    private EffectRenderHelper effectRenderHelper;
    private Context mContext;
    private VideoView videoView;
    private MediaController mediaController;
    private ImageButton mVideoButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        if (!isResourceReady()){
            unzipTask = new UnzipTask(this);
            unzipTask.execute("resource.zip");
        }
        LogUtils.i("Is resource ready: " + isResourceReady());
        checkPermissions();
        ResourceHelper.init(this);

//        mVideoButton = findViewById(R.id.videobutton);
//        mVideoButton.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });
        mSurfaceView = findViewById(R.id.gl_surface);
        videoView = findViewById(R.id.videoView);
        mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.yoga);
        effectRenderHelper = mSurfaceView.getEffectRenderHelper();
        effectRenderHelper.setMainActivity(MainActivity.this);
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.setLooping(true);
            }
        });
//        videoView.start();
        CameraDevice.get().setMainActivity(this);



//        sRootDir = this.getExternalFilesDir("assets");
//        mSkeletonDetector = new SkeletonDetect();
//        int initResult = mSkeletonDetector.init(this, getSkeletonModelPath(), getLicensePath(LICENSE_NAME));
//        LogUtils.i("Skeleton class init result: " + initResult);
    }
    @Override
    protected void onResume() {
        super.onResume();
        // setup device
        mSurfaceView.onResume();
//        videoView.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        CameraDevice.get().setPreviewSize(0,0);
    }

    boolean isResourceReady(){
        SharedPreferences preferences = this.getSharedPreferences("user", Context.MODE_PRIVATE);
        boolean resourceReady = preferences.getBoolean("resource", false);
        return resourceReady;
    }

    @Override
    protected void onPause() {
        super.onPause();
        mSurfaceView.onPause();
    }

    public static String getSkeletonModelPath() {
        File file = new File(new File(sRootDir, SKELETON), "");
        return file.getAbsolutePath();
    }

    public static String getLicensePath(String path) {
        File file = new File(new File(sRootDir, "license"), path);
        return file.getAbsolutePath();
    }
    private void checkPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED &&
                    checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            } else {
                // start Permissions activity
                Intent intent = new Intent(this, PermissionsActivity.class);
                startActivity(intent);
                finish();
                overridePendingTransition(0, 0);
            }
        }
    }


}
